package com.example.servlet;

import com.example.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CookieServlet extends BaseServlet {
    @Override
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       Cookie[] cookies = req.getCookies();
       for(Cookie cookie:cookies){
           //getName方法返回Cookie的key
           //getValue方法返回cookie的value值
           resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]");
       }
    }
    @Override
    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建cookie对象
        Cookie cookie =  new Cookie("key1","value1");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie);
        resp.getWriter().write("Cookie创建成功");
    }
}
